import { Get_Infomation, Add_Infomation, Editor_Infomation, Editor_infomation } from "../actionsType";
import request from "../../utils/request";
import {
    message,
} from 'antd';


const inistate = {
    Infomationlist: [],
    from: {
        title: "",
        originator: "",
        content: "",
        id: "",
    }
}

const Information = (state = inistate, action) => {
    const { type, payload } = action;
    let { Infomationlist, from } = state;
    let newress;
    switch (type) {
        //获取
         //获取
         case Get_Infomation:
            Infomationlist = payload;
            return {
                ...state,
                Infomationlist,
            };
        //新增
        case Add_Infomation:
            var now = new Date();
            var year = now.getFullYear(); //得到年份
            var month = now.getMonth() + 1; //得到月份
            var date = now.getDate(); //得到日期
            var hour = now.getHours(); //得到小时数
            var minute = now.getMinutes(); //得到分钟数
            var second = now.getSeconds(); //得到秒数
            var newdate = `${year}-${month}-${date} ${hour}:${minute}:${second}`;
            newress = request({
                url: "/api/addInfomation",
                method: "post",
                data: {
                    arr: { ...from, date: newdate },
                }
            });
            newress.then(res => {
                if (res.code === 200) {
                    message.success(res.message);
                } else {
                    message.error(res.message);
                }
            })
            return {
                ...state,
            };
            //编辑
            case Editor_Infomation:
            now = new Date();
            year = now.getFullYear(); //得到年份
            month = now.getMonth() + 1; //得到月份
            date = now.getDate(); //得到日期
            hour = now.getHours(); //得到小时数
            minute = now.getMinutes(); //得到分钟数
            second = now.getSeconds(); //得到秒数
            newdate = `${year}-${month}-${date} ${hour}:${minute}:${second}`;
            newress = request({
                url: "/api/editorInfomation",
                method: "put",
                data: {
                    arr: { ...from, date:newdate },
                }
            });
            newress.then(res => {
                if (res.code === 200) {
                    message.success(res.message);
                } else {
                    message.error(res.message);
                }
            })
            return {
                ...state,
            };
        case Editor_infomation:
            const { type, text } = payload;
            if (type) {
                from[type] = text;
            } else {
                from = {
                    title: payload.title,
                    originator: payload.originator,
                    content: payload.content,
                    id: payload.id,
                }
            }
            return {
                ...state,
                from,
            };
        default:
            return {
                ...state
            }
    }
}

export default Information